//
//  MineView.swift
//  jingxin
//
//  Created by Yuki on 2020/8/10.
//  Copyright © 2020 Zl. All rights reserved.
//

import UIKit

class MineView: BaseView ,TNCollectionViewLayoutDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource{
    override init(frame: CGRect) {
        super.init(frame: frame);
        buildUI();
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder);
        buildUI();
    }
    
    //MARK:创建界面
    private func buildUI(){
        
        /**  添加瀑布流  **/
        self.addSubview(self.mainCollectionView);
        self.mainCollectionView.snp_makeConstraints { (make) in
            make.left.right.bottom.top.equalTo(0);
        };
        
    }
    
    //MARK:CollectionView代理
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 100;
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1;
    }
    
    /**  获取item的高度  **/
    func waterFlowLayout(layout: TNCollectionViewLayout, indexPath: NSIndexPath, itemWidth: CGFloat) -> CGFloat {
        //这里写成随机数会每次都有变化，这里是错误的
        return CGFloat(arc4random()%200+100);
    }

    /**  item的行间距  **/
    func rowMarginInWaterFlowLayout(layout: TNCollectionViewLayout) -> CGFloat {
        return 10.0;
    }

    /**  item的列间距  **/
    func columnMarginInWaterFlowLayout(layout: TNCollectionViewLayout) -> CGFloat {
        return 10.0;
    }

    /**  列数  **/
    func columnCountInWaterFlowLayout(layout: TNCollectionViewLayout) -> Int {
        return 2
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(MineCell.classForCoder()), for: indexPath) as! MineCell;
        
        return cell
    }
    
//
//    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
//        /**  注册的头部  **/
//        if kind == UICollectionView.elementKindSectionHeader{
//            let header = collectionView.dequeueReusableSupplementaryView(ofKind:UICollectionView.elementKindSectionHeader, withReuseIdentifier: NSStringFromClass(MineCollectionHeaderView.classForCoder()), for: indexPath);
//            header.frame = CGRect(x: 0, y: 0, width: ScreenW(), height: 40.0)
//            return header;
//        }
//        return UICollectionReusableView();
//    }
//
//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
//        return CGSize(width: ScreenW(), height: 40.0);
//    }
    
    
    //MARK:lazy
    /**  瀑布流  **/
    private lazy var mainCollectionView: UICollectionView = {
        var layout = TNCollectionViewLayout();
        layout.delegate = self;
//        var layout = UICollectionViewFlowLayout();
//        layout.scrollDirection = .vertical;
        var mainCollectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout);
        mainCollectionView.backgroundColor = .white;
        mainCollectionView.delegate = self;
        mainCollectionView.dataSource = self;
        mainCollectionView.register(MineCell.classForCoder(), forCellWithReuseIdentifier:NSStringFromClass(MineCell.classForCoder()));
//        mainCollectionView.register(MineCollectionHeaderView.classForCoder(), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: NSStringFromClass(MineCollectionHeaderView.classForCoder()));
        return mainCollectionView;
    }();
    
}
